﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.Visuals;
using System.IO;
using System.Reflection;

namespace FileExplorer
{
    /// <summary>
    /// This control is a chunk for an Item inside the lens of a <see cref="T:LensView"/>.
    /// </summary>
    public class StartMenuDetailChunk : ListViewItem
    {
        private Label titleLabel;
        private Label detailLabel;
        private PictureBox image;

        public StartMenuDetailChunk()
            : base()
        {
            titleLabel = new Label { Font = StartMenuItem.Font, Shadowed = true, TextColor = Color.White, LineAlignment = Alignment.Center };
            detailLabel = new Label { Shadowed = true, TextColor = Color.White, LineAlignment = Alignment.Near, LineMode = LineMode.WordWrapEndElipsis, };
            image = new PictureBox { Size = new Size(84, 84), Margin = new Margin(null, null, 8, null), Visible = false };
            Controls.Add(image);
            Controls.Add(titleLabel);
            Controls.Add(detailLabel);
            ShowBorderLine = false;
        }

        public string Title { get { return titleLabel.Text; } set { titleLabel.Text = value; Invalidate(InvalidationFlag.Appearance); } }

        public string Details { get { return detailLabel.Text; } set { detailLabel.Text = value; Invalidate(InvalidationFlag.Appearance); } }

        public Texture Image { get { return image.Image; } set { image.Image = value; Invalidate(InvalidationFlag.Appearance); } }

        public string ImageResourcePath
        {
            set
            {
                Texture img = new Texture();
                Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(value);
                if (stream == null) throw new ArgumentNullException("ImageResourcePath");
                img.LoadFromStream(stream);
                Image = img;

            }
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Appearance | InvalidationFlag.Size))
            {
                int left = 8;
                int w = Width - 8;
                if (Image != null)
                {
                    image.Visible = true;
                    w -= image.Size.Width - 4;
                    image.Location = new Point(w + 4, (Height - image.Size.Height) / 2);
                }


                if (!string.IsNullOrEmpty(Details))
                {
                    detailLabel.Visible = true;
                    titleLabel.Bounds = new Rectangle(left, 4, w, titleLabel.Font.CharHeight + 2);
                    detailLabel.Bounds = new Rectangle(left, titleLabel.Bottom + 4, w, Height - 2 - titleLabel.Bottom - 4);
                }
                else
                {
                    detailLabel.Visible = false;
                    titleLabel.Bounds = new Rectangle(left, 4, w, Height - 8);
                }
            }
            base.OnLayout();
        }
    }
}
